package com.erp.module.faHuoDanJu.faHuo2

import android.content.Context
import android.util.Log
import com.erp.bean.FaHuoBean
import com.erp.Interface.OnUserDataInterface
import com.erp.module.faHuoDanJu.AddFaHuoModel
import com.erp.module.faHuoDanJu.FaHuoDanJuContract
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/11/16/016.
 * 作者：刘洋
 */
class IFaHuoCarKP (val context: Context, val KView: FaHuoDanJuContract.FaHuoCarKView, var fahuobean: FaHuoBean): FaHuoDanJuContract.FaHuoCarKP {
    val modle = AddFaHuoModel()

    override fun postshopweight() {
        modle.setcarweight(context,fahuobean,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                Log.d("SouSuoCar","aaaa="+data)
                if(data==null){
                    KView.SouSuoCarNo()
                }else{
                    Myutil.MyTost("修改排序成功")
                    jsondata(data)
                    KView.SouSuoCarOk()
                }
            }

            override fun onNot(throwable: String?) {
                Log.d("SouSuoCar",throwable)
                KView.SouSuoCarNo()
            }

        })
    }

    override fun postonumweight(positiong : Int) {
        modle.setshoponumweight(context,fahuobean,positiong,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                Log.d("SouSuoCar","aaaa="+data)
                if(data==null){
                    KView.SouSuoCarNo()
                }else{
                    Myutil.MyTost("修改排序成功")
                    jsondata(data)
                    KView.SouSuoCarOk()
                }
            }

            override fun onNot(throwable: String?) {
                Log.d("SouSuoCar",throwable)
                KView.SouSuoCarNo()
            }

        })
    }


    override fun SouSuoCar(car: String) {
        modle.getdanju(context,car,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                Log.d("SouSuoCar",Myutil.decodeUnicode(data))
                if(data==null){
                    KView.SouSuoCarNo()
                }else{
                    jsondata(data)
                    KView.SouSuoCarOk()
                }
            }

            override fun onNot(throwable: String?) {
                Log.d("SouSuoCar",throwable)
                KView.SouSuoCarNo()
            }
        })
    }
    //解析json数据
    fun jsondata(data : String){
        Myutil.longlogshow("jsondata",Myutil.decodeUnicode(data));
        fahuobean.shops.clear()
        try {
            val jo = JSONObject(data)
            fahuobean.car_number=JsonUtil.getvolue(jo,"car_number")
            fahuobean.location=JsonUtil.getvolue(jo,"location")
            fahuobean.car_name= JsonUtil.getvolue(jo,"car_name")
            fahuobean.fullcarload=JsonUtil.getvolue(jo,"fullcarload")
            fahuobean.plan_deliver_time=JsonUtil.getvolue(jo,"plan_deliver_time")
            fahuobean.remark=JsonUtil.getvolue(jo,"remark")
            fahuobean.dcn_id=JsonUtil.getvolue(jo,"dcn_id")

            val shopjo = JsonUtil.getjovolue(jo,"shop")
            val keys = shopjo.keys()
            while (keys.hasNext()) {
                val key = keys.next() as String
                val bean = FaHuoBean.shop()
                val jophoto = JsonUtil.getjovolue(shopjo,key)
                bean.shop_name= JsonUtil.getvolue(jophoto,"shop_name")
                var jsonum = JsonUtil.getjsvolue(jophoto,"wonums")
                for (i in 0 until jsonum.length()){
                    var joonum = jsonum.get(i) as JSONObject
                    var onum = FaHuoBean.onum()
                    onum.exclusive_shop = JsonUtil.getvolue(joonum,"exclusive_shop")
                    onum.oid = JsonUtil.getvolue(joonum,"oid")
                    onum.onum = JsonUtil.getvolue(joonum,"onum")
                    onum.weight = JsonUtil.getint(joonum,"weight")
                    bean.onums.add(onum)
                    Myutil.longlogshow("jsondata",joonum.toString());
                }
//                bean.onums= JsonUtil.getvolue(jophoto,"onums")
                bean.exclusive_shop= JsonUtil.getvolue(jophoto,"exclusive_shop")
                bean.pcount= JsonUtil.getvolue(jophoto,"p_count")
                bean.weight = JsonUtil.getint(jophoto,"weight")
                bean.wuliu_gz = JsonUtil.getvolue(jophoto,"wuliu_gz")
                val jsdv = JsonUtil.getjsvolue(jophoto,"dvfiles")
                for(i in 0 until jsdv.length()){
                    val jodv = jsdv.get(i) as JSONObject
                    val photo = FaHuoBean.photo()
                    photo.fid=JsonUtil.getvolue(jodv,"fid")
                    photo.url=JsonUtil.getvolue(jodv,"fm_uri")
                    bean.dvlist.add(photo)
                }
                val jsdl = JsonUtil.getjsvolue(jophoto,"dlfiles")
                for (i in 0 until jsdl.length()){
                    val jodl = jsdl.get(i) as JSONObject
                    val photo = FaHuoBean.photo()
                    photo.fid=JsonUtil.getvolue(jodl,"fid")
                    photo.url=JsonUtil.getvolue(jodl,"fm_uri")
                    bean.dllist.add(photo)
                }
                fahuobean.shops.add(bean)
            }

        }catch (e : JSONException){
            e.printStackTrace()
            Log.d("jsondata",e.toString())
        }
        Log.d("jsondata",fahuobean.toString())
    }


}